/**
 * 
 */
package com.icbc.ctpdemo.user;

import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

import com.icbc.cte.base.TranFailException;
import com.icbc.cte.logging.Log;
import com.icbc.cte.logging.LogFactory;
import com.icbc.ctp.core.IContext;
import com.icbc.ctp.core.annotations.ExceptionCode;
import com.icbc.ctp.core.annotations.Output;
import com.icbc.ctp.core.impl.OperationStep;
import com.icbc.ctp.infra.security.ext.BASE64Decoder;
import com.icbc.ctp.infra.security.ext.BASE64Encoder;

/**
 *用户信息加密
 * <pre>
 *
 * </pre>
 * <pre>
 * modify by sunming on 2015-6-12
 *    fix->1.
 *         2.
 * </pre> 
 */
@Output(name = { "decodePwd","isValid" }, description = { "加密后的密码","是否通过校验" })
public class AddUserDes extends OperationStep {

	  private static Log ctpLog = LogFactory.getCtpLog(AddUserDes.class);
	  private  String encroptMode = "ECB";
	  private  String paddingMode = "PKCS5Padding";
	  private  String algorithm = "DES";
	  private  String keyStr = "ICBC_iras_1234";
//	  private  String keyStr = "A+wPztnlGyUXvuTzJHdRf/T7zKbPyhp1ckLrOT3P+sUPR08aZO8Uo0aMSHQ3DQ9KCytqFWkOwaoo";
	  private  String oriDataName = "";
	  private String encryptStr = "";
	  
	  @ExceptionCode(errorCode={"EncryptDataOpStep001"}, description={"data encryption failed"})
	  public int execute(IContext context)throws TranFailException
	  {

	    try
	    {
	      ctpLog.info("测试加密开始====================\n\n");
	      UserBean user = (UserBean) context.get("user");
	      if(user!=null){
	    	  oriDataName = user.getPassword();
	    	  if(oriDataName!=null && !"".equals(oriDataName)){
		    	  ctpLog.info("新增用户密码调用开始===oriDataName前="+oriDataName);
		    	  oriDataName = encodePassword(oriDataName);
		    	  user.setPassword(oriDataName);
		    	  context.setValueAt("user", user);
		    	  
		    	  context.setValueAt("decodePwd",oriDataName); 
		    	  context.setValueAt("isValid",true); 
		    	  ctpLog.info("新增用户密码调用开始===oriDataName后="+oriDataName);
	    	  }
	      }
	      
	      String pw = (String) context.get("pw");//如果是登录调用
	      if(pw !=null && !"".equals(pw)){
	    	  ctpLog.info("登录用户密码调用开始===pw前="+pw);
	    	  pw = encodePassword(pw);
	    	  context.setValueAt("pw", pw);
	    	  ctpLog.info("登录用户密码调用开始===pw后="+pw);
	      }
	      
	      //
	      String useroldPassword = (String) context.get("oldPassword");//如果是修改用户密码调用
	      String password = (String) context.get("password");//如果是修改用户密码调用
	      if(useroldPassword!=null && !"".equals(useroldPassword)){
	    	  ctpLog.info("修改用户密码调用开始===oldPassword前="+useroldPassword);
	    	  useroldPassword = encodePassword(useroldPassword);
	    	  context.setValueAt("oldPassword", useroldPassword);
	    	  ctpLog.info("修改用户密码调用开始===oldPassword后="+useroldPassword);
	    	  ctpLog.info("修改用户密码调用开始===password前="+password);
	    	  password = encodePassword(password);
	    	  context.setValueAt("password", password);
	    	  
	    	  context.setValueAt("decodePwd",oriDataName); 
	    	  context.setValueAt("isValid",true); 
	    	  ctpLog.info("修改用户密码调用开始===password后="+password);
	      }
	      
	      ctpLog.info("\n\n");
	      ctpLog.info("测试加密结束====================");
	    }catch (Exception e){
		      TranFailException ctee = new TranFailException("EncryptDataOpStep001", "EncryptDataOpStep.execute()", "data encryption failed", e, new Object[] { algorithm, keyStr });
	
		      inException(context, ctee);
		      return 1;
		 }

	    return 0;
	  }
	  
	  
	  public String encodePassword(String password) throws Exception{
	      BASE64Decoder base64decoder = new BASE64Decoder();
	      BASE64Encoder base64encoder = new BASE64Encoder();
	      byte[] keyBytes = base64decoder.decodeBuffer(keyStr);
	      KeyGenerator generator = KeyGenerator.getInstance(algorithm);
	      generator.init(new SecureRandom(keyBytes));
	      Key key = generator.generateKey();
	      Cipher cipher = Cipher.getInstance(algorithm + "/" + encroptMode + "/" + paddingMode);
	      
	      cipher.init(1, key);
	      byte[] encryptBytes = cipher.doFinal(password.getBytes("UTF-8"));
	      
	      encryptStr = base64encoder.encode(encryptBytes);
		  
	      return encryptStr;
	  }

}
